package com.ybkj.o2o.zhuanche.controller;

import com.google.common.base.Preconditions;

import com.ybkj.o2o.zhuanche.common.transport.Error;
import com.ybkj.o2o.zhuanche.common.transport.Result;
import com.ybkj.o2o.zhuanche.entity.PayRecordOrder;
import com.ybkj.o2o.zhuanche.manager.PayRecordOrderManager;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("api/payRecordOrder")
public class PayRecordOrderRes {

  @Autowired
  private PayRecordOrderManager payRecordOrdermanager;


  /**
   * 添加充值记录.
   * @param orderId 订单id
   * @param money 操作金额
   * @param payRecordTypeOrder 支付方式
   */
  @RequestMapping(value = "create", method = RequestMethod.POST)
  public ResponseEntity<Object> create(
      @RequestParam("orderId") Long orderId,
      @RequestParam("money") Double money,
      @RequestParam("payRecordTypeOrder") String payRecordTypeOrder,
      @RequestParam("appKey") String appKey) {
    ResponseEntity<Object> responseEntity = null;
    appKey = StringUtils.trimToEmpty(appKey);
    Preconditions.checkArgument(StringUtils.isNotBlank(appKey), "AppKey不能为空");
    Preconditions.checkArgument(orderId != null, "订单Id不能为空");

    try {
      PayRecordOrder payRecordOrder =
          payRecordOrdermanager.insert(orderId, money, payRecordTypeOrder);

      Result<PayRecordOrder> result = new Result<PayRecordOrder>(0, "成功", payRecordOrder);

      responseEntity = new ResponseEntity<Object>(result, HttpStatus.CREATED);

      return responseEntity;

    } catch (Exception ex) {
      ex.printStackTrace();
      Error error = new Error(500, "内部服务器错误：" + ex.getMessage());
      responseEntity = new ResponseEntity<Object>(error, HttpStatus.INTERNAL_SERVER_ERROR);
      return responseEntity;
    }

  }

  /**
   * 查询充值记录.
   * @param id 订单id
   */
  @RequestMapping(value = "findOne", method = RequestMethod.GET)
  public ResponseEntity<Object> findOne(
      @RequestParam("id") Long id,
      @RequestParam("appKey") String appKey) {
    ResponseEntity<Object> responseEntity = null;
    appKey = StringUtils.trimToEmpty(appKey);
    Preconditions.checkArgument(StringUtils.isNotBlank(appKey), "AppKey不能为空");
    try {
      PayRecordOrder payRecordOrder = payRecordOrdermanager.findByIdAndAppKey(id);

      Result<PayRecordOrder> result = new Result<PayRecordOrder>(0, "成功", payRecordOrder);

      responseEntity = new ResponseEntity<Object>(result, HttpStatus.OK);

      return responseEntity;
    } catch (Exception ex) {
      ex.printStackTrace();
      Error error = new Error(500, "内部服务器错误：" + ex.getMessage());
      responseEntity = new ResponseEntity<Object>(error, HttpStatus.INTERNAL_SERVER_ERROR);
      return responseEntity;
    }
  }

  /**
   * 修改财务记录为已交易成功.
   * @param id 财务id
   */
  @RequestMapping(value = "update", method = RequestMethod.PUT)
  public ResponseEntity<Object> update(
      @RequestParam("id") Long id,
      @RequestParam("appKey") String appKey) {
    ResponseEntity<Object> responseEntity = null;

    appKey = StringUtils.trimToEmpty(appKey);
    Preconditions.checkArgument(StringUtils.isNotBlank(appKey), "AppKey不能为空");

    try {
      PayRecordOrder payRecordOrder = payRecordOrdermanager.update(id);

      Result<PayRecordOrder> result = new Result<PayRecordOrder>(0, "成功", payRecordOrder);
      responseEntity = new ResponseEntity<Object>(result, HttpStatus.CREATED);
      return responseEntity;
    } catch (Exception ex) {
      ex.printStackTrace();
      Error error = new Error(500, "内部服务器错误：" + ex.getMessage());

      responseEntity = new ResponseEntity<Object>(error, HttpStatus.INTERNAL_SERVER_ERROR);

      return responseEntity;
    }
  }

}
